package com.reny.ll.git.base_logic.app_task

import android.os.Looper
import android.util.Log
import com.reny.ll.git.base_logic.app_task.cockroach.Cockroach
import com.reny.ll.git.base_logic.app_task.cockroach.ExceptionHandler
import com.reny.ll.git.core.App
import com.reny.ll.git.core.taskdispatch.task.MainTask


class InitCockroach : MainTask() {

    //override fun runOnMainThread(): Boolean = true
    private val TAG = "CockroachException"

    override fun run() {
        if(!App.isDebug) {
            Cockroach.install(App.instance, object : ExceptionHandler() {
                override fun onUncaughtExceptionHappened(
                    thread: Thread,
                    throwable: Throwable?
                ) {
                    Log.e(
                        TAG,
                        "AndroidRuntime--->onUncaughtExceptionHappened:$thread<---",
                        throwable
                    )
                    /*try {
                        MobclickAgent.reportError(App.instance, throwable)
                        UMCrash.generateCustomLog(throwable, "CockroachException")
                    }catch (e: Exception){e.printStackTrace()}*/
                }

                override fun onBandageExceptionHappened(throwable: Throwable) {
                    throwable.printStackTrace() //打印警告级别log，该throwable可能是最开始的bug导致的，无需关心
                }

                override fun onEnterSafeMode() {
                    Log.e(TAG, "$TAG onEnterSafeMode")
                }

                override fun onMayBeBlackScreen(e: Throwable?) {
                    val thread = Looper.getMainLooper().thread
                    Log.e(TAG, "AndroidRuntime--->onMayBeBlackScreen:$thread<---", e)
                    //黑屏时建议直接杀死app
                    //AppManager.instance.exitApp(App.instance)
                    /*try {
                        MobclickAgent.reportError(App.instance, e)
                        UMCrash.generateCustomLog(e, "CockroachException")
                    }catch (e: Exception){e.printStackTrace()}*/
                }
            })
        }

    }

}